package com.mmlc.inter;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.androidnetframe.BaseParentActivity;

/**
 * @author king
 * @function Fragment抽象基类
 * @date 2016-1-14上午9:27:29
 */
public abstract class InterFragment extends Fragment implements OnClickListener {
    protected long lastTime = 0;
    protected int lastViewId = 0;

    protected boolean avoid(View view) {
        return avoid(view, 700, null);
    }

    protected boolean avoid(View view, String toastStr) {
        return avoid(view, 700, toastStr);
    }


    protected boolean avoid(View view, long period, String toastStr) {
        long currentTime = System.currentTimeMillis();
        if (currentTime - lastTime <= period && view.getId() == lastViewId) {
            if (context != null) {
            }
            return true;
        } else {
            lastTime = currentTime;
            lastViewId = view.getId();
        }
        return false;
    }

    protected View view;
    protected String TAG;
    protected BaseParentActivity context;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(initResourceID(), container, false);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        TAG = this.getClass().getSimpleName();
        lastTime = System.currentTimeMillis();
        context = (BaseParentActivity) getActivity();

        initExtra();
        initView();
        initInfo();
        initEvent();
        registerReceiver();
    }

    /**
     * 绑定布局
     *
     * @return resourceID
     */
    protected abstract int initResourceID();

    /**
     * 获取参数
     */
    protected void initExtra() {
    }

    /**
     * 初始化控�?
     */
    protected abstract void initView();

    /**
     * 加载数据
     */
    protected void initInfo() {
    }

    /**
     * 注册事件
     */
    protected void initEvent() {
    }

    /**
     * 注册广播接收�?
     */
    protected void registerReceiver() {
    }

    @Override
    public void onClick(View v) {
        if (avoid(v))
            return;
    }
}
